<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/8/3
 * Time: 11:35
 */

namespace app\commi\controller;

use app\api\controller\Initmatch;
use app\api\controller\Yacht;
use think\Db;
use think\Request;

class Chouq extends Base
{
    /**
     * 抽签列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->field('match_id')
            ->find();
        $group = Db::table('up_match_apply_group')
            ->join('up_match_group', 'mg_id = mag_group_id', 'left')
            ->where('mag_match_id', $match['match_id'])
            ->field('mg_id, mg_name')
            ->select();
        if(request()->isAjax ()){
            $group_id = input('group_id'); //  组
            $boat_type = input('boat_type'); // 船型
            $distance = input('distance'); // 赛道距离
            
            $where['ma_match_id'] = $match['match_id'];
            $where['ma_role'] = 1;
            // 组别
            if(isset($group_id) && $group_id != ''){
                $where['ma_group'] = $group_id;
            }
            /*else{
                $where['ma_group'] = $group[0]['mg_id'];
            }*/
            // 船型
            if(isset($boat_type) && $boat_type != ''){
                $where['ma_boat_type'] = $boat_type;
            }
            /*else{
                $where['ma_boat_type'] = 1;
            }*/
            // 赛道距离
            if(isset($distance) && $distance != ''){
                $where['ma_distance'] = $distance;
            }
            /*else{
                $where['ma_distance'] = 500;
            }*/
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('match_apply_info')
                ->join('up_club', 'ma_club_id = club_id', 'left')
                ->join('up_match_group', 'mg_id = ma_group', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->join('up__match_turns', 'mt_id = ma_turns_id', 'left')
                ->join('up__match_rounds', 'mr_id = ma_rounds_id', 'left')
                ->where($where)
//                ->group('ma_team_name')
                ->count();// 获取总条数
            $lists = Db::name('match_apply_info')
                ->join('up_club', 'ma_club_id = club_id', 'left')
                ->join('up_match_group', 'mg_id = ma_group', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->join('up__match_turns', 'mt_id = ma_turns_id', 'left')
                ->join('up__match_rounds', 'mr_id = ma_rounds_id', 'left')
                ->field('ma_id, match_status, club_name, mg_name, ma_team_name, ma_distance, ma_boat_type, 
                ma_dao_num, mt_name, mr_name, ma_lots')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('ma_time desc')
//                ->group('ma_team_name')
                ->select();
//            foreach($lists as $k => $v){
//                $lists[$k]['member_type'] = session('cm_member_type');
//            }
            $where_str = json_encode($where);
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists, 'where_str' => $where_str]);
        }
        
        $this->assign([
            'group' => $group
        ]);
        return $this->fetch();
    }
    
    /**
     * 编辑组次和轮次
     * @return mixed|\think\response\Json
     */
    public function writeZu(){
        $ma_id = input('ma_id');
        if(Request::instance()->isPost()){
            $ma_turns_id = input('ma_turns_id');
            $ma_rounds_id = input('ma_rounds_id');
            $apply = Db::table('up_match_apply_info')
                ->where('ma_id', $ma_id)
                ->field('ma_match_id, ma_team_name')
                ->find();
            $res = Db::table('up_match_apply_info')
                ->where('ma_match_id', $apply['ma_match_id'])
                ->where('ma_team_name', $apply['ma_team_name'])
                ->update([
                    'ma_turns_id' => $ma_turns_id,
                    'ma_rounds_id' => $ma_rounds_id
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        // 组次
        $rounds = Db::table('up__match_rounds')->field('mr_id, mr_name')->select();
        // 轮次
        $turns = Db::table('up__match_turns')->field('mt_id, mt_name')->select();
        $this->assign([
           'rounds' => $rounds,
            'turns' => $turns,
            'ma_id' => $ma_id
        ]);
        return $this->fetch('chouq/writezu');
    }
    
    /**
     * 编辑场次
     * @return \think\response\Json
     */
    public function updateChang(){
        $ma_id = input('id');
        $field = input('field');
        $value = trim(input('value'), ' ');
        $apply = Db::table('up_match_apply_info')
            ->where('ma_id', $ma_id)
            ->field('ma_match_id, ma_team_name')
            ->find();
        if($field == 'ma_dao_num'){ // 编辑的是赛道
            $match = Db::table('up_match')->where('match_id', $apply['ma_match_id'])->field('match_saidao_num')->find();
            if($value > $match['match_saidao_num']){
                return json(['code' => 100, 'msg' => '最大赛道数为' . $match['match_saidao_num']]);
            }
        }
        $res = Db::table('up_match_apply_info')
            ->where('ma_match_id', $apply['ma_match_id'])
            ->where('ma_team_name', $apply['ma_team_name'])
            ->update([$field => $value]);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 抽签
     * @return \think\response\Json
     */
    public function chouQian(){
        $where = input('ids');
        $where = json_decode($where, true);
        if(!isset($where['ma_match_id']) || !$where['ma_match_id']) return json(['code' => 100, 'msg' => '没有赛事']);
        if(!isset($where['ma_group']) || !$where['ma_group']) return json(['code' => 100, 'msg' => '请选择组别']);
        if(!isset($where['ma_boat_type']) || !$where['ma_boat_type']) return json(['code' => 100, 'msg' => '请选择船型']);
        if(!isset($where['ma_distance']) || !$where['ma_distance']) return json(['code' => 100, 'msg' => '请选择赛道距离']);
        
        $lists = Db::name('match_apply_info')->field('ma_id')->where($where)->select();
        if(!$lists) return json(['code' => 100, 'msg' => '没有相关数据']);
    
        // 随机排序数组
        $keys = array_keys($lists);
        shuffle($keys);
        // 给参赛队排号
        foreach($lists as $k => $v){
            foreach($keys as $kk => $vv){
                if($vv == $k){
                    $num = $kk + 1;
                    Db::table('up_match_apply_info')->where('ma_id', $v['ma_id'])->setField('ma_lots', $num);
                }
            }
        }
        // 初始化参赛信息
        Db::startTrans();
        try{
            $initMatch = new Initmatch();
            $initMatch->initMatch($where['ma_match_id']);
            $yacht = new Yacht();
            $res = $yacht->initYacht($where['ma_match_id'], $where['ma_group'], $where['ma_boat_type'], $where['ma_distance']);
            if($res['status'] == 200){
                Db::commit();
                return json(['code' => 200, 'msg' => $res['msg']]);
            }else{
                Db::rollback();
                return json(['code' => 100, 'msg' => $res['msg']]);
            }
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
}